// pages/realName/realName.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fontImg: '../../images/zhengmian.jpg',
    reverseImg: '../../images/fanmian.jpg',
    certificates: ['身份证', '军官证', '学生证'],
    index: null,
    m_pic1: '',
    m_pic2: '',
  },

  // 证件类型
  certificatesChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },

  // 上传照片
  uploadCardImg: function (e) {
    let that = this
    let index = e.currentTarget.dataset.index
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths[0]
        if (index == 1) {
          wx.uploadFile({
            filePath: tempFilePaths,
            name: 'file',
            url: 'https://wx.zhonghuazhengxin.com/api/Upload/pic',
            success: res => {
              let result = JSON.parse(res.data)
              console.log(result)
              that.data.m_pic1 = result.result_data
            }
          })
          that.setData({
            fontImg: tempFilePaths
          })
        } else {
          wx.uploadFile({
            filePath: tempFilePaths,
            name: 'file',
            url: 'https://wx.zhonghuazhengxin.com/api/Upload/pic',
            success: res => {
              let result = JSON.parse(res.data)
              console.log(result)
              that.data.m_pic2 = result.result_data
            }
          })
          that.setData({
            reverseImg: tempFilePaths
          })
        }
      }
    })
  },

  // 实名认证
  personSubmit: function (e) {
    let obj = e.detail.value    
    obj.token = wx.getStorageSync('token')
    obj.m_pic1 = this.data.m_pic1
    obj.m_pic2 = this.data.m_pic2
    console.log(obj);

    if (obj.m_name == '' || obj.m_card == '' || obj.m_salary == '' || obj.m_phone == '' || obj.m_pic1 == '' || obj.m_pic2 == '') {
      wx.showToast({
        title: '请完善资料',
        icon: 'none'
      })
    } else {
      app.post('Real/member', obj).then(() => {
        wx.showModal({
          title: '提示',
          content: '提交成功,请等待审核',
          showCancel: false,
          success: res => {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1,
              })
            }
          }
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})